﻿Imports System.Configuration
Imports System.IO
Imports System.Web.Mail
Namespace helper
    ''' <summary>
    ''' provides methods due with SMTP Email
    ''' </summary>
    ''' <remarks></remarks>
    Public Class SMTPEmailHelper

        Private Shared _email As String = String.Empty
        Private Shared _password As String = String.Empty
        Private Shared _smtp_server As String = String.Empty
        Private Shared _smtp_port As String = String.Empty
        Private Shared _sendusing As String = String.Empty
        Private Shared _smtp_authenticate As String = String.Empty
        Private Shared _smtp_usess1 As Boolean = True




        ''' <summary>
        ''' determine which email service
        ''' </summary>
        ''' <param name="mailType">email service</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Shared Function emailServiceFactory(ByVal mailType As eEmailService)
            Select Case mailType
                Case eEmailService.gmail
                    _smtp_server = "smtp.gmail.com"
                    _smtp_port = "465"
                    _sendusing = "2"
                    _smtp_authenticate = "1"
                    _smtp_usess1 = True
                Case eEmailService.hotmail
                    _smtp_server = "smtp.live.com"
                    _smtp_port = "587"
                    _sendusing = "2"
                    _smtp_authenticate = "1"
                    _smtp_usess1 = True
                Case eEmailService.ymail
                    _smtp_server = "plus.smtp.mail.yahoo.com"
                    _smtp_port = "465"
                    _sendusing = "2"
                    _smtp_authenticate = "1"
                    _smtp_usess1 = True
            End Select
        End Function

        ''' <summary>
        ''' Send Email
        ''' </summary>
        ''' <param name="senderemail">sender email</param>
        ''' <param name="senderpassword">sender email password</param>
        ''' <param name="send_to">recipient email</param>
        ''' <param name="subject">email subject</param>
        ''' <param name="body">email content</param>
        ''' <remarks></remarks>
        Public Shared Sub SendMail(ByVal senderemail As String, ByVal senderpassword As String, ByVal send_to As String, ByVal subject As String, ByVal body As String)
            _email = senderemail
            _password = senderpassword
            Dim pVeify As Boolean = False
            'check para
            If String.IsNullOrEmpty(send_to) Then
                Throw New ArgumentNullException("send_to")
            End If

            If String.IsNullOrEmpty(subject) Then
                Throw New ArgumentNullException("subject")
            End If

            If String.IsNullOrEmpty(body) Then
                Throw New ArgumentNullException("body")
            End If
            Try


                Dim myMail As New System.Web.Mail.MailMessage()
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", _smtp_server)
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _smtp_port)
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", _sendusing)
                'sendusing: cdoSendUsingPort, value 2, for sending the message using 
                'the network.

                'smtpauthenticate: Specifies the mechanism used when authenticating 
                'to an SMTP 
                'service over the network. Possible values are:
                '- cdoAnonymous, value 0. Do not authenticate.
                '- cdoBasic, value 1. Use basic clear-text authentication. 
                'When using this option you have to provide the user name and password 
                'through the sendusername and sendpassword fields.
                '- cdoNTLM, value 2. The current process security context is used to 
                ' authenticate with the service.
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", _smtp_authenticate)
                'Use 0 for anonymous
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _email)
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password)
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", _smtp_usess1)

                myMail.From = _email
                myMail.[To] = send_to
                myMail.Subject = subject
                myMail.BodyFormat = MailFormat.Html

                System.Web.Mail.SmtpMail.SmtpServer = _smtp_server & ":" & _smtp_port
                System.Web.Mail.SmtpMail.Send(myMail)
            Catch ex As Exception
                Throw ex
            End Try

        End Sub


        Public Enum eEmailService
            gmail
            hotmail
            ymail
        End Enum


    End Class
End Namespace

